/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package filecopier.gui.scripteditor;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JButton;
import javax.swing.JScrollPane;

import java.util.Vector;
import java.util.List;

import filecopier.script.Script;
import filecopier.script.ScriptEntry;

public class RestoreScriptViewerDialog extends JDialog {

    public static final long serialVersionUID = 1;
    private String scriptName = null;
    private JPanel jContentPane = null;
    private JLabel scriptLabel = null;
    private JList scriptContentList = null;
    private JScrollPane scrollPane = null;
    private JButton okBtn = null;
    private JFrame owner = null;
    private Script script = null;

    /**
     * This is the default constructor
     */
    public RestoreScriptViewerDialog(final Script script, boolean modal) {
        super(filecopier.Singleton.getInstance().getFrame(), script != null ? script.getScriptName() : "Untitled", modal);
        this.script = script;
        initialize();
    }

    /**
     * This method initializes this
     * 
     * @return void
     */
    private void initialize() {
        this.setContentPane(getJContentPane());
        this.pack();
        this.setVisible(true);
    }

    /**
     * This method initializes jContentPane
     * 
     * @return javax.swing.JPanel
     */
    private JPanel getJContentPane() {
        if (jContentPane == null) {
            jContentPane = new JPanel();
            jContentPane.setLayout(new BorderLayout(10, 10));
            jContentPane.add(getScrollPane(), java.awt.BorderLayout.CENTER);
            jContentPane.add(getOkBtn(), java.awt.BorderLayout.SOUTH);
        }
        return jContentPane;
    }

    /**
     * This method initializes scriptContentList	
     * 	
     * @return javax.swing.JList	
     */
    private JList getScriptContentList() {
        if (scriptContentList == null) {
            Vector<String> scriptContents = loadScriptContentList();
            if (scriptContents != null) {
                scriptContentList = new JList(scriptContents);
            } else {
                scriptContentList = new JList();
            }
        }
        return scriptContentList;
    }

    /**
     * This method initializes okBtn	
     * 	
     * @return javax.swing.JButton	
     */
    private JButton getOkBtn() {
        if (okBtn == null) {
            okBtn = new JButton("OK");
            okBtn.setMnemonic(java.awt.event.KeyEvent.VK_O);
            okBtn.addActionListener(new java.awt.event.ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    dispose();
                }
            });
        }
        return okBtn;
    }

    private Vector<String> loadScriptContentList() {
        Vector<String> scriptContents = null;
        List<ScriptEntry> scriptEntries = null;

        if (script != null && (scriptEntries = script.getScriptEntries()) != null) {
            scriptContents = new Vector<String>(scriptEntries.size());
            for (ScriptEntry se : scriptEntries) {
                String entry = "Restore \"" + se.getPath(ScriptEntry.PathType.Restore) + "\" to \"" +
                        se.getPath(ScriptEntry.PathType.Delete) + "\"";
                scriptContents.add(entry);
            }
        }

        return scriptContents;
    }

    private JScrollPane getScrollPane() {
        if (scrollPane == null) {
            scrollPane = new JScrollPane(getScriptContentList());
        }
        return scrollPane;
    }
}
